23 template <class T
> string
toStr(const T
&x
){
24 stringstream s
; s
<< x
; return s
.str();
27 template <class T
> int toInt(const T
&x
){
28 stringstream s
; s
<< x
; int r
; s
>> r
; return r
;
31 #define For(i, a, b) for (int i=(a); i<(b); ++i)
32 #define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x)
33 #define D(x) cout << #x " = " << (x) << endl
35 const double EPS
= 1e-9;
36 int cmp(double x
, double y
, double tol
= EPS
){
37 return (x
<= y
+ tol
) ? (x
+ tol
< y
) ? -1 : 0 : 1;
40 const int oo
= INT_MAX
/ 2;
42 #define minimize(x, y) if ((y) < (x)) (x) = (y)
51 inline int inserts(int a
, int b
){
52 if (b
< a
) swap(a
, b
);
54 if (diff
== 0) return 0;
55 if (M
== 0) return oo
;
56 return ((diff
- 1) / M
) * I
;
60 scanf("%d%d%d%d", &D
, &I
, &M
, &n
);
61 for (int i
= 0; i
< n
; ++i
){
65 for (int k
= 0; k
< 256; ++k
){
69 for (int i
= 1; i
<= n
; ++i
){
71 for (int k
= 0; k
< 256; ++k
){
73 int extra
= abs(k
- item
);
74 for (int j
= i
- 1; j
>= 0; --j
){
75 int deleted
= i
- j
- 1;
76 for (int p
= 0; p
< 256; ++p
){
77 minimize(dp
[i
][k
], extra
+ inserts(p
, k
) + deleted
* D
+ dp
[j
][p
]);
83 // for (int i = 0; i <= n; ++i){
84 // for (int k = 0; k < 16; ++k){
85 // printf("dp[%d][%d] = %d\n", i, k, dp[i][k]);
90 for (int k
= 0; k
< 256; ++k
){
91 minimize(ans
, dp
[n
][k
]);
100 for (int i
= 0; i
< T
; ++i
){
101 printf("Case #%d: ", i
+ 1);